package com.home.manager.utils;


import com.home.manager.exception.ApplicationError;
import org.springframework.util.StringUtils;

import java.util.Base64;

public class Encoded {

    /**
     * base64 加签
     *
     * @param source 原字符串
     * @return base64加签后字符串
     * @TODO 可以加盐
     */
    public static String base64(String source) {
        if (!StringUtils.hasText(source)) {
            return source;
        }
        try {

            byte[] originalBytes = source.getBytes();
            return Base64.getEncoder().encodeToString(originalBytes);
        } catch (Exception e) {
            throw new ApplicationError("");
        }
    }

    public static void main(String[] args) {
        String s = base64("{\"userId\":123456,\"accountName\":\"王菲\",\"role\":\"admin\"}");
        System.out.println(s);
    }


}
